/* eslint-disable no-unused-vars */
/*
       1. 数组求和及平均值
       let arr1 = [7, 2, 1, 17, 23];
      */
let arr1 = [7, 2, 1, 17, 23];
let sum1 = 0;
let ave1 = 0;
arr1.forEach(function (ele) {
    sum1 += ele;
});
ave1 = sum1 / arr1.length;
console.log('sum1', sum1);
console.log('ave1', ave1);

/*
       2. 求数组[12,52,12,7,123,5,17]中的最大值。
       */
let arr2 = [12, 52, 12, 7, 123, 5, 17];
let max;
max = arr2.reduce(function (total, ele) {
    if (ele > total) return ele;
    else return total;
}, arr2[0]);
console.log('max', max);
/*
       3. 将数组 ['red', 'green', 'blue', 'pink'] 里面的元素转换为字符串, 输出“redgreenbluepink”
       */
let arr3 = ['red', 'green', 'blue', 'pink'];
let str3;
str3 = arr3.reduce(function (total, ele) {
    return total + ele;
}, '');
console.log('str3', str3);
/*
       4. 将数组 ['red', 'green', 'blue', 'pink'] 转换为字符串，并且用 | 或其他符号分割, 输出: 'red|green|blue|pink'
       */
let arr4 = ['red', 'green', 'blue', 'pink'];
let str4 = arr4.join('|');
console.log('str4', str4);
/*
         5. 把数组[2,5,6,7,8]中的每一位数字都增加30%，并返回一个新数组
         */
let arr5 = [2, 5, 6, 7, 8];
let newArray5 = [];
// eslint-disable-next-line no-unused-vars
newArray5 = arr5.map(function (ele, index, arr5) {
    return (ele * 13) / 10;
});
console.log('newArray5', newArray5);
/*
         6. 编写函数has(arr, 20) 判断数组中是否存在20这个元素，返回布尔类型
         */
// let arr6 = [16, 21, 23, 3, 5, 1, 5, 48, 1, 23];//false
let arr6 = [16, 21, 23, 3, 5, 1, 5, 48, 1, 23, 20]; //true
function has(arr, n) {
    return arr.some(function (ele) {
        return ele === 20;
    });
}
console.log(has(arr6, 20));
/*
         7. 根据数组中对比age的年龄, 从大到小排序
         let arr7 = [
       {age: 19, name:'a'},
       {age: 50, name:'b'},
       {age: 12, name:'c'},
       {age: 25, name:'d'}
         ]
         */
let arr7 = [
    { age: 19, name: 'a' },
    { age: 50, name: 'b' },
    { age: 12, name: 'c' },
    { age: 25, name: 'd' },
];
let newArray7 = [];
newArray7 = arr7.sort(function (a, b) {
    return b.age - a.age;
});
console.log('newArray7', newArray7);
/*
         8. 改变传入的数组，将数组中第 n(从 0 开始算 ) 个元素放到数组的开头
         */
let arr8 = [1, 2, 3, 4, 5];
let n = 1;
arr8.forEach(function (ele, index, arr) {
    if (index === n) {
        arr.splice(n, 1);
        arr.unshift(ele);
    }
});
console.log('arr8', arr8);
/*
         9. 将 函数中的arguments 对象转换成真正的数组
         */
let newArray9 = [];
function fn() {
    newArray9 = Array.from(arguments);
}
fn(1, 2, 3, 4, 5, 'red');
console.log('newArray9', newArray9);
/*
         10. 如何实现一个数组push方法
         */
let arr10 = [1, 23, 6, 54, 8, 9, 6];
let arr10Add = '';
function pushEle(arr, data) {
    let newArray10;
    newArray10 = arr.concat(arr10Add);
    newArray10.splice(newArray10.lastIndexOf(''), 1, data);
    return newArray10;
}

console.log(pushEle(arr10, 3));

/*
         11. 数组去重的方法 (4种)
         */
let arr11 = [1, 2, 3, 1, 5, 8, 7, 2, 5, 6, 4, 2];
// 1.forEach
let newArray11a = [];
arr11.forEach(function (ele, index) {
    if (!newArray11a.includes(ele)) {
        newArray11a.push(ele);
    }
});
console.log(newArray11a);

// 2.filter,但是不是按照顺序输出，对应的数字只会取最后一次出现的位置
let newArray11b = [];
newArray11b = arr11.filter(function (ele, index, arr) {
    if (!arr.includes(ele, index + 1)) {
        return ele;
    }
});
console.log(newArray11b);
// 3.map和filter连用，但是也不会按顺序输出，对应的数字只会取最后一次出现的位置
let newArray11c = [];
newArray11c = arr11
    .map(function (ele, index, arr) {
        if (!arr.includes(ele, index + 1)) {
            return ele;
        }
    })
    .filter(function (ele) {
        if (ele !== 'indefined') {
            return ele;
        }
    });
console.log(newArray11c);
// 4.reduce
let newArray11d = [];
newArray11d = arr11.reduce(function (total, current) {
    if (!total.includes(current)) {
        total.push(current);
    }
    return total;
}, []);
console.log(newArray11d);
